from game_shared import SHARED_SERVER_PORT
from player_client import PlayerClient
import time, random

#=======================================================================================================================
# This is where you should implement your Artificial Intelligence
# You have the ability to implement two Ai's:
#   1. The Ai for the shots you'll make against your opponent.
#   2. The Ai for placing your ships.
#
# There is a supplementary method, called reinitialize(), which is called in PlayerClient right before
# boards are exchanged betweens the players.
#=======================================================================================================================

class Me(PlayerClient):
    def __init__(self, a,b,c ):
        PlayerClient.__init__(self, a, b, c)
        
        self.some_value_stored = 67     # Maybe this value will need to be reset before each game?
                                        # Look at reinitialize method below.
    
    def shoot_algorithm(self):
        ''' 
            Should return a tuple representing coordinate to shoot at.
            You are allowed to shoot at cells you've already shot at.
            If you shoot at a cell that is not a valid cell, you'll automatically lose that game.'
        '''
        
        # ------
        # Us the following line to obtain your opponent's boat placement for the last game.
        # Will obviously return None if called before one game has been played.
        # -------
        opponents_shots = self.get_opponents_last_game_shots()
        
        # ------
        # You can also check if you last shot was a hit.
        # as well as get the coords of your last shot.
        # ------
        last_shot_hit = self.is_last_shot_hit()
        last_shot = self.get_last_shot()
        
        # -----
        # Get a list of the shots you've taken
        # -----
        shots_taken = self.get_shots()
        
        # -----
        # Need a list of all valid cells to shoot at?  Here you go.
        # -----
        valid_cells = self.get_valid_cells()
        
        
        # ------
        # Below is shoot_algorithm() AI that checks if the last shot was a hit.
        # If it was, the AI continues to shoot around that coordinate.
        # If it wasn't a hit, the AI chooses a new random coordinate. 
        # -------       
        coords = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        
        shoot_at = (-1,-1)
        cont = False
        x = 0
            
        if last_shot_hit:
            while not cont and x < 150:
                x += 1
                index = valid_cells.index( last_shot )
                if index != 0 and index != 100:
                    if random.random() < 0.5:
                        index += 1
                    else:
                        index -=1
                    shoot_at = valid_cells[index]
                elif index == 0:
                    shoot_at = valid_cells[index+1]
                else:
                    shoot_at = valid_cells[index-2]
                
                if shoot_at not in shots_taken:
                    cont = True
                     
        else:
            while 1:  
                x = random.randint(0,9)
                y = random.randint(0,9)
                shoot_at = ( x, y )
                
                if len(self.shots) > 100: break                         # Ignore the check if you've shot over 50.
                if shoot_at not in shots_taken: break                           
        
        return shoot_at
        
    def boat_place_algorithm(self):
        '''  
            Should return a list of 5 boats. 
            In the format: boats = [ [ length, x_coord, y_coord, orientation ] x 5 ]
        '''
        boat_coords = [ [ 3, 0, 0, "h" ], [ 3, 0, 1, "h"], [ 4, 0, 2, "h" ], [ 4, 0, 3, "h" ], [ 5, 0, 4, "h" ] ] 
        return boat_coords
    
    def reinitialize(self):
        ''' Allows one to reinitialize their local variables before each game.'''
        self.some_value_stored = 0
        

#=======================================================================================================================
# Run the AI.
#=======================================================================================================================
me = Me( "redsea", "me", SHARED_SERVER_PORT )
me.enter_game_client()
me.run()
